package com.meicunbing.viberplugin;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import com.meicunbing.viberplugin.com.Weather;
import com.meicunbing.viberplugin.db.CityListDbAdapter;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class CityListActivity extends Activity {

	@SuppressWarnings("unchecked")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_citylist);
		// 绑定Layout里面的ListView
		ListView list = (ListView) findViewById(R.id.cityCodeListView);
		CityListDbAdapter cityListDbAdapter = new CityListDbAdapter(this);
		cityListDbAdapter.open();
		Cursor cur = cityListDbAdapter.getAllCityCode();
		Log.i("cur", "共有记录" + cur.getCount());

		// 生成动态数组，加入数据
		ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
		if (cur.getCount() > 0) {
			while (cur.moveToNext()) {
				HashMap<String, Object> map = new HashMap<String, Object>();
				map.put("cityNameTextView", cur.getString(cur
						.getColumnIndex(CityListDbAdapter.KEY_CITY)));
				map.put("cityCodeTextView", cur.getString(cur
						.getColumnIndex(CityListDbAdapter.KEY_CODE)));
				listItem.add(map);
			}
		} else {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("cityNameTextView", "暂无城市信息");
			map.put("cityCodeTextView", "");
			listItem.add(map);
			long result = cityListDbAdapter.createCityCode("南京", "101190101");
			Log.i("cur", "插入记录结果" + result);
		}
		cur.close();
		cityListDbAdapter.close();
		// 生成适配器的Item和动态数组对应的元素
		SimpleAdapter listItemAdapter = new SimpleAdapter(this, listItem,// 数据源
				R.layout.city_list_item,// ListItem的XML实现
				// 动态数组与ImageItem对应的子项       
				new String[] { "cityNameTextView", "cityCodeTextView" },
				// ImageItem的XML文件里面的一个ImageView,两个TextView ID
				new int[] { R.id.cityNameTextView, R.id.cityCodeTextView });
		// 添加并且显示
		list.setAdapter(listItemAdapter);
		// 添加点击
		list.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				setTitle("点击第" + arg2 + "个项目");
				ListView listView = (ListView) arg0;
				HashMap<String, String> map = (HashMap<String, String>) listView
						.getItemAtPosition(arg2);
				final String city_code = map.get("cityCodeTextView");
				new Thread() {
					public void run() {
						Looper.prepare();
						Weather w = new Weather();
						w.setCityCode(city_code);
						JSONObject wo = w.getfWeather();
						if (wo == null) {
							CityListActivity.this.showWeather("暂无天气预报"
									.toString());
							return;
						}
						try {
							CityListActivity.this.showWeather(wo
									.getString("city")
									+ wo.getString("date_y")
									+ wo.getString("week")
									+ ":"
									+ wo.getString("temp1")
									+ wo.getString("weather1")
									+ wo.getString("fx1")
									+ wo.getString("wind1")
									+ wo.getString("index")
									+ wo.getString("index_d"));
						} catch (JSONException e) {
							e.printStackTrace();
						}
						Looper.loop();
					}
				}.start();
			}
		});

		// 添加长按点击
		list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {

			@Override
			public void onCreateContextMenu(ContextMenu menu, View arg1,
					ContextMenuInfo arg2) {
				menu.setHeaderTitle("长按菜单-ContextMenu");
				menu.add(0, 0, 0, "弹出长按菜单0");
				menu.add(0, 1, 0, "弹出长按菜单1");

			}
		});
	}

	public void showWeather(String s) {
		Toast.makeText(CityListActivity.this, s, Toast.LENGTH_LONG).show();
	}

	// 长按菜单响应函数
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		setTitle("点击了长按菜单里面的第" + item.getItemId() + "个项目");
		return super.onContextItemSelected(item);
	}

}
